<template>
  <div>
    <el-dialog
      title="首课截图"
      v-model="inSchooleTextVis" 
      width="700"
      @close="diaClose"
    >
      <el-form :model="addSchoolPlanForm">
        <el-form-item label="首课截图：" :label-width="100">
          <el-upload
            class="upload-demo"
            :action="`${$config.API_URL}common/upload/`"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove" 
            list-type="picture-card"
            :on-success="handleAvatarSuccess"
            accept=".png,.jpeg,.jpg"
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList"
            type="success"
          >
            <el-button size="small" type="success">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <el-dialog v-model="dialogVisible" :destroy-on-close="true">
        <img  style="width: 100%;" :src="fileList[0].url" alt="Preview Image" />
      </el-dialog>
      <template #footer>
        <div style="display: flex;  ">
          <el-button style="width: 100%;" @click="handleClose" :loading="loadingSave" type="success" plain>取消</el-button>
        <el-button
        style="width: 100%;"
          type="success"
          @click="submitDataS"
          :loading="loadingSave"
          :disabled="loadingTitle == '详情'"
          >确定</el-button
        >
        </div>
        
      </template>
    </el-dialog>
  </div>
</template>
  
  <script>
import {  fristimage} from "@/api/api";
import { ElMessage } from "element-plus";

export default {
  data() {
    return {
      inSchooleTextVis: false,
      addSchoolPlanForm: {
        id: "",
        images: [],
        videos: [],
        description: "",
      },
      fileList: [],
      videoList: [],
      loadingSave: false,
      loadingTitle: "",
      dialogVisible: false,
    };
  },
  methods: {
    handleError(e) {
      console.log(e, "----------报错信息");
    },
    handleClose() {
      this.inSchooleTextVis = false;
    },
    addPlan(e) {
      if (e.frist_image) {
        this.fileList = [{name:e.frist_image,url:e.frist_image}];
      }
     
      this.inSchooleTextVis = true;
      this.addSchoolPlanForm.id = e.id;
    },
    diaClose() {
      this.fileList = [];
      this.videoList = [];
    },
    submitDataS() {
      let vm = this;
       if(this.fileList.length==0){
         return ElMessage.error("首课截图不能为空！！！");
       }
      fristimage({ id: this.addSchoolPlanForm.id, frist_image: this.fileList[0].url }).then((res) => {
        if (res.code == 1) { 
          vm.$message.success(res.msg);

          vm.inSchooleTextVis = false;
          vm.$emit("getData");
        }
      });
    },
    handlePreview(file) {
      //console.log(file);
      this.dialogVisible=true;
    },
    handleRemove(file, fileList) { 
      this.fileList=[]
      // this.fileList = this.fileList.filter((item) => {
      //   return item.name != file.name;
      // });
      //console.log(this.fileList);
    },
    handleRemoveVideo(file, fileList) {
      this.videoList = this.videoList.filter((item) => {
        return item.name != file.name;
      });
      //console.log(this.videoList);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除该截图？`);
    },
    handleAvatarSuccess(res, file) { 
      this.fileList.push({
        name: res.data.url,
        url: res.data.fullurl,
      });
    },
    handleAvatarSuccessVideo(res, file) {
      this.videoList.push({
        name: res.data.url,
        url: res.data.fullurl,
      });
    },
    handleExceed(files, fileList) {
      ElMessage.warning("只能上传一张首课截图");
    },
  },
};
</script>